package db

import (
	"gitee.com/ameise84/e3log"
	"github.com/redis/go-redis/v9"
	"gorm.io/gorm"
)

func RegisterTableMetaFactory(f Factory) {
	registerFactory(f)
}

func Start() (c Cli, err error) {
	if n := _gRedisInstCount.Add(1); n == 1 {
		_gDBCli = &client{}
		err = _gDBCli.start()
		if err == nil {
			_gDBLogger.Info("start redis service").Println()
		}
	}
	return _gDBCli, err
}

func Stop() {
	old := _gDBCli
	if n := _gRedisInstCount.Add(-1); n == 0 {
		_gDBLogger.Info("stop redis service").Println()
		old.stop()
	}
}

func GetInstance() Cli {
	if _gDBCli == nil {
		return nil
	}
	return _gDBCli
}

func GetRedis() redis.UniversalClient {
	if _gDBCli == nil {
		return nil
	}
	return _gDBCli.cli
}
func GetDumper() *gorm.DB {
	if _gDBCli == nil {
		return nil
	}
	return _gDBCli.dump.dr
}

func SetLogger(log *e3log.Logger) {
	_gDBLogger = log
}

func GetLogger() *e3log.Logger {
	return _gDBLogger
}
